package com.ruoyi.event.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.event.domain.bo.EventThBo;
import com.ruoyi.event.domain.vo.EventThVo;
import com.ruoyi.event.service.IEventThService;
import com.ruoyi.system.service.ISysConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 谈话记录
 *
 * @author strongme
 * @date 2023-02-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/event/th")
public class EventThController extends BaseController {

    private final IEventThService iEventThService;
    private final ISysConfigService sysConfigService;

    /**
     * 谈话模版接口
     *
     * @return
     */
    @GetMapping("tpl")
    public R<JSONArray> getTpls() {
        String configValue = sysConfigService.selectConfigByKey("th.tpl");
        return R.ok(JSONUtil.parseArray(configValue));
    }

    /**
     * 查询谈话记录列表
     */
    @GetMapping("/list")
    public TableDataInfo<EventThVo> list(EventThBo bo, PageQuery pageQuery) {
        return iEventThService.queryPageList(bo, pageQuery);
    }

    @PostMapping("query")
    public R<List<EventThVo>> query(@RequestBody EventThBo bo) {
        return R.ok(iEventThService.queryList(bo));
    }

    /**
     * 导出谈话记录列表
     */
    @Log(title = "谈话记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EventThBo bo, HttpServletResponse response) {
        List<EventThVo> list = iEventThService.queryList(bo);
        ExcelUtil.exportExcel(list, "谈话记录", EventThVo.class, response);
    }

    /**
     * 获取谈话记录详细信息
     *
     * @param thId 主键
     */
    @GetMapping("/{thId}")
    public R<EventThVo> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long thId) {
        return R.ok(iEventThService.queryById(thId));
    }

    /**
     * 新增谈话记录
     */
    @Log(title = "谈话记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EventThBo bo) {
        return toAjax(iEventThService.insertByBo(bo));
    }

    /**
     * 修改谈话记录
     */
    @Log(title = "谈话记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EventThBo bo) {
        return toAjax(iEventThService.updateByBo(bo));
    }

    /**
     * 删除谈话记录
     *
     * @param thIds 主键串
     */
    @Log(title = "谈话记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{thIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] thIds) {
        return toAjax(iEventThService.deleteWithValidByIds(Arrays.asList(thIds), true));
    }
}
